"use strict";
const eling = (function (global) {
    if (typeof global == 'undefined' || global != window || !global.document) {
        throw new Error("不支持的运行环境,请在浏览器中运行(Unsupported environment,Please run in browser)");
    }
    function getWebGLInfo() {
        try {
            const canvas = document.createElement('canvas');
            const gl = canvas.getContext('webgl');
            if (!gl)
                return 'no-webgl';
            const renderer = gl.getExtension(gl.RENDERER) || gl.getExtension('WEBGL_debug_renderer_info');
            let glInfo = "unknown";
            if (renderer) {
                glInfo = `${gl.getParameter(renderer.UNMASKED_VENDOR_WEBGL)}|${gl.getParameter(renderer.UNMASKED_RENDERER_WEBGL)}`;
            }
            const spf = gl.getShaderPrecisionFormat(gl.FRAGMENT_SHADER, gl.HIGH_FLOAT);
            const info = {
                extensions: gl.getSupportedExtensions(),
                version: gl.getParameter(gl.VERSION),
                renderer: gl.getParameter(gl.RENDERER),
                vendor: gl.getParameter(gl.VENDOR),
                maxTextureSize: gl.getParameter(gl.MAX_TEXTURE_SIZE),
                maxRenderbufferSize: gl.getParameter(gl.MAX_RENDERBUFFER_SIZE),
                shaderPrecision: spf ? spf.precision : 0,
                precisionFormatRange: spf ? `${spf.rangeMin}-${spf.rangeMax}` : '0',
                maxVertexAttribs: gl.getParameter(gl.MAX_VERTEX_ATTRIBS),
                glInfo
            };
            return getHash(JSON.stringify(info));
        }
        catch (e) {
            if (e instanceof Error) {
                return `webgl-error:${e.message}`;
            }
            return 'webgl-unknown-error';
        }
    }
    function generateCanvasDataHex() {
        const canvas = document.createElement('canvas');
        const ctx = canvas.getContext('2d');
        if (!ctx) {
            return 'no-canvas';
        }
        canvas.style.width = '256px';
        canvas.style.height = '128px';
        ctx.textBaseline = 'top';
        ctx.font = '14px Arial';
        ctx.fillStyle = '#f60';
        ctx.fillRect(125, 1, 62, 20);
        ctx.fillStyle = '#069';
        ctx.fillText('abcdefghijklmnopqrstuvwxyz$#%&', 2, 15);
        ctx.font = '16px Arial';
        ctx.fillStyle = 'rgba(102, 204, 0, 0.7)';
        ctx.fillText('*&^%%$#@!qwertyuiopasdfghjklzxcvbnm', 4, 20);
        ctx.fillStyle = 'rgba(0, 21, 255, 0.5)';
        ctx.fillRect(112, 36, 45, 16);
        const dataUrl = canvas.toDataURL();
        canvas.remove();
        return getHash(dataUrl);
    }
    function getAudioInfo() {
        try {
            const context = new AudioContext();
            const oscillator = context.createOscillator();
            const analyser = context.createAnalyser();
            const gainNode = context.createGain();
            const audio = {
                sampleRate: context.sampleRate,
                maxChannelCount: context.destination.maxChannelCount,
                baseLatency: context.baseLatency,
                outputLatency: context.outputLatency,
                workletSupport: typeof context.audioWorklet !== 'undefined',
                oscillatorType: oscillator.type,
                analyserFFTSize: analyser.fftSize,
                gainValue: gainNode.gain.value,
                audioBufferSupport: typeof context.createBuffer !== 'undefined',
                currentTime: context.currentTime,
                contextState: context.state || 'unknown'
            };
            oscillator.disconnect();
            analyser.disconnect();
            gainNode.disconnect();
            context.close();
            return getHash(JSON.stringify(audio));
        }
        catch (e) {
            if (e instanceof Error) {
                return `generatea audio fingerprint error:${e.message}`;
            }
            return 'audio-fingerprint-unknown-error';
        }
    }
    async function generateAudioRenderingHex() {
        function _calculateMean(data) {
            return data.reduce((sum, x) => sum + x, 0) / data.length;
        }
        function _calculateVariance(data) {
            const mean = _calculateMean(data);
            return data.reduce((sum, x) => sum + Math.pow(x - mean, 2), 0) / data.length;
        }
        function _calculateZeroCrossing(data) {
            let count = 0;
            for (let i = 1; i < data.length; i++) {
                const a = data[i];
                const b = data[i - 1];
                if (a * b < 0)
                    count++;
            }
            return count / data.length;
        }
        const sampleRate = 44100;
        const context = new window.OfflineAudioContext(1, sampleRate, sampleRate);
        const oscillator = context.createOscillator();
        const compressor = context.createDynamicsCompressor();
        const analyser = context.createAnalyser();
        oscillator.type = 'sine';
        oscillator.frequency.value = 10000;
        compressor.threshold.value = -50;
        compressor.knee.value = 40;
        compressor.ratio.value = 20;
        compressor.attack.value = 0.1;
        compressor.release.value = 0.25;
        oscillator.connect(compressor);
        compressor.connect(analyser);
        analyser.connect(context.destination);
        oscillator.start(0);
        const audioBuffer = await context.startRendering();
        const channelData = audioBuffer.getChannelData(0);
        const samplePoints = [
            channelData[200] || 0,
            channelData[500] || 0,
            channelData[4400] || 0,
            channelData[22000] || 0
        ];
        const features = [
            channelData.length,
            ...samplePoints,
            _calculateMean(channelData),
            _calculateVariance(channelData),
            _calculateZeroCrossing(channelData.slice(0, 1000))
        ];
        oscillator.disconnect();
        compressor.disconnect();
        analyser.disconnect();
        const intFeatures = features.map(f => Math.floor(Math.abs(f) * 1e14));
        return getHash(intFeatures);
    }
    function getFontsInfo() {
        const fonts = new Set([
            "Calibri", "Cambria", "Consolas", "Corbel", "Segoe UI", "Microsoft YaHei",
            "DengXian", "SimHei", "SimSun", "KaiTi", "FangSong",
            "SF Pro", "SF Mono", "Apple SD Gothic Neo", "PingFang SC", "Hiragino Sans", "Osaka",
            "DejaVu Sans", "DejaVu Sans Mono", "Ubuntu", "Cantarell",
            "Arial", "Times New Roman", "Courier New", "Verdana", "Georgia", "Comic Sans MS", "Impact",
            "sans-serif", "sans-serif-thin", "serif", "monospace", "system-ui",
            "ARNO PRO", "中易宋体", "仿宋", "华文细黑", "宋体", "微软雅黑", "雅黑", "黑体",
            "Agency FB", "Arabic Typesetting", "AvantGarde Bk BT", "Bahnschrift", "BankGothic Md BT",
            "Batang", "Bitstream Vera Sans Mono", "Book Antiqua", "Bookman Old Style", "Bradley Hand ITC",
            "Century", "Clarendon", "Constantia", "Courier", "EUROSTILE", "Ebrima", "Franklin Gothic",
            "Freestyle Script", "French Script MT", "Futura Bk BT", "GOTHAM", "Gabriola", "Gadugi",
            "Garamond", "Gill Sans MT", "HELV", "Haettenschweiler", "Helvetica",
            "Humanst521 BT", "Ink Free", "Javanese Text", "Juice ITC", "Kristen ITC", "Leelawadee",
            "Letter Gothic", "Levenim MT", "Lucida Bright", "MS PGothic", "MS UI Gothic",
            "Meiryo UI", "Menlo", "MingLiU-ExtB", "Minion Pro", "Monotype Corsiva", "PMingLiU", "SCRIPTINA",
            "Segoe UI Emoji", "Serifa", "Sitka Banner", "Staccato222 BT", "Sylfaen", "Symbol",
            "TRAJAN PRO", "Tahoma", "Tempus Sans ITC", "Trebuchet MS", "Univers CE 55 Medium", "Vrinda",
            "Webdings", "Yu Gothic", "ZWAdobeF"
        ]);
        const testElement = document.createElement('div');
        const baseElement = document.createElement('div');
        testElement.textContent = baseElement.textContent = "abcdefghijklmnopqrstuvwxyz";
        document.body.insertAdjacentElement('beforeend', testElement);
        document.body.insertAdjacentElement('beforeend', baseElement);
        const baseStyle = {
            position: 'absolute',
            left: '-9999px',
            fontSize: '24px',
            visibility: 'hidden',
            fontVariant: 'normal',
            fontStyle: 'normal',
            fontWeight: 'normal'
        };
        Object.assign(baseElement.style, baseStyle, {
            fontFamily: 'monospace'
        });
        function _checkFontSupport(fontName) {
            Object.assign(testElement.style, baseStyle, {
                fontFamily: `'${fontName.replace(/'/g, '\\\'')}', 'monospace'`
            });
            const testWidth = testElement.clientWidth;
            const baseWidth = baseElement.clientWidth;
            return Math.abs(testWidth - baseWidth) > 1;
        }
        const availableFonts = new Array();
        fonts.forEach(font => {
            if (_checkFontSupport(font)) {
                availableFonts.push(font);
            }
        });
        testElement.remove();
        baseElement.remove();
        return getHash(availableFonts.sort());
    }
    function getWgslLanguageFeaturesInfo() {
        var _a;
        const arr = new Array();
        const wgslFeatures = (_a = navigator.gpu) === null || _a === void 0 ? void 0 : _a.wgslLanguageFeatures;
        if (wgslFeatures && wgslFeatures.keys) {
            arr.push(...Array.from(wgslFeatures.keys()));
        }
        return getHash(arr);
    }
    function compatibleInfo() {
        const arr = new Array();
        if (navigator.plugins) {
            arr.push(...Array.from(navigator.plugins).map(plugin => plugin.name));
        }
        if (navigator.mimeTypes) {
            arr.push(...Array.from(navigator.mimeTypes).map(mimeType => mimeType.type));
        }
        if (navigator.platform) {
            arr.push(navigator.platform);
        }
        arr.push(Math.acos(0.123424234234234242), Math.acosh(1e208), Math.sinh(1), Math.cos(10.000000000123), Math.asin(0.123424234234234242), Math.asinh(1), Math.atanh(0.5), Math.atan(0.5), Math.sin(-1e300), Math.tan(-1e300));
        arr.push(!!window.localStorage, !!window.sessionStorage, !!window.indexedDB, navigator.cookieEnabled);
        return getHash(arr.sort());
    }
    function getHash(data) {
        if (Array.isArray(data)) {
            data = data.join('|');
        }
        return sha256(data);
    }
    function sha256(message) {
        const K = new Uint32Array([
            0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
            0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
            0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
            0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
            0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
            0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
            0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
            0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
            0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
            0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
            0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
            0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
            0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
            0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
            0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
            0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
        ]);
        function rotr(n, x) {
            return (x >>> n) | (x << (32 - n));
        }
        function _sum0(x) {
            return rotr(2, x) ^ rotr(13, x) ^ rotr(22, x);
        }
        function _sum1(x) {
            return rotr(6, x) ^ rotr(11, x) ^ rotr(25, x);
        }
        function _sigma0(x) {
            return rotr(7, x) ^ rotr(18, x) ^ (x >>> 3);
        }
        function _sigma1(x) {
            return rotr(17, x) ^ rotr(19, x) ^ (x >>> 10);
        }
        function _ch(x, y, z) {
            return (x & y) ^ (~x & z);
        }
        function _maj(x, y, z) {
            return (x & y) ^ (x & z) ^ (y & z);
        }
        function _stringToUTF8Bytes(str) {
            const bytes = [];
            for (let i = 0; i < str.length; i++) {
                let code = str.charCodeAt(i);
                if (code > 0xd7ff && code < 0xe000) {
                    if (i + 1 < str.length) {
                        const extra = str.charCodeAt(i + 1);
                        if ((extra & 0xfc00) === 0xdc00) {
                            code = ((code & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000;
                            i++;
                        }
                    }
                }
                if (code < 0x80) {
                    bytes.push(code);
                }
                else if (code < 0x800) {
                    bytes.push(0xc0 | (code >> 6));
                    bytes.push(0x80 | (code & 0x3f));
                }
                else if (code < 0x10000) {
                    bytes.push(0xe0 | (code >> 12));
                    bytes.push(0x80 | ((code >> 6) & 0x3f));
                    bytes.push(0x80 | (code & 0x3f));
                }
                else {
                    bytes.push(0xf0 | (code >> 18));
                    bytes.push(0x80 | ((code >> 12) & 0x3f));
                    bytes.push(0x80 | ((code >> 6) & 0x3f));
                    bytes.push(0x80 | (code & 0x3f));
                }
            }
            return bytes;
        }
        function _bytesToHex(bytes) {
            return bytes.map(b => b.toString(16).padStart(2, '0')).join('');
        }
        function _padMessage(message) {
            const bitLength = message.length * 8;
            const padded = [...message];
            padded.push(0x80);
            while ((padded.length % 64) !== 56) {
                padded.push(0x00);
            }
            const lengthBuffer = new ArrayBuffer(8);
            const lengthView = new DataView(lengthBuffer);
            lengthView.setUint32(0, 0, false);
            lengthView.setUint32(4, bitLength, false);
            for (let i = 0; i < 8; i++) {
                padded.push(lengthView.getUint8(i));
            }
            return padded;
        }
        function _bytesToWords(bytes) {
            const words = new Uint32Array(16);
            for (let i = 0; i < 16; i++) {
                const offset = i * 4;
                words[i] = (bytes[offset] << 24) |
                    (bytes[offset + 1] << 16) |
                    (bytes[offset + 2] << 8) |
                    (bytes[offset + 3]);
            }
            return words;
        }
        let bytes;
        if (typeof message === 'string') {
            bytes = _stringToUTF8Bytes(message);
        }
        else {
            bytes = message;
        }
        const padded = _padMessage(bytes);
        let h0 = 0x6a09e667;
        let h1 = 0xbb67ae85;
        let h2 = 0x3c6ef372;
        let h3 = 0xa54ff53a;
        let h4 = 0x510e527f;
        let h5 = 0x9b05688c;
        let h6 = 0x1f83d9ab;
        let h7 = 0x5be0cd19;
        for (let i = 0; i < padded.length; i += 64) {
            const block = padded.slice(i, i + 64);
            const words = _bytesToWords(block);
            const w = new Uint32Array(64);
            for (let t = 0; t < 16; t++) {
                w[t] = words[t];
            }
            for (let t = 16; t < 64; t++) {
                w[t] = (_sigma1(w[t - 2]) + w[t - 7] + _sigma0(w[t - 15]) + w[t - 16]) >>> 0;
            }
            let a = h0;
            let b = h1;
            let c = h2;
            let d = h3;
            let e = h4;
            let f = h5;
            let g = h6;
            let h = h7;
            for (let t = 0; t < 64; t++) {
                const T1 = (h + _sum1(e) + _ch(e, f, g) + K[t] + w[t]) >>> 0;
                const T2 = (_sum0(a) + _maj(a, b, c)) >>> 0;
                h = g;
                g = f;
                f = e;
                e = (d + T1) >>> 0;
                d = c;
                c = b;
                b = a;
                a = (T1 + T2) >>> 0;
            }
            h0 = (h0 + a) >>> 0;
            h1 = (h1 + b) >>> 0;
            h2 = (h2 + c) >>> 0;
            h3 = (h3 + d) >>> 0;
            h4 = (h4 + e) >>> 0;
            h5 = (h5 + f) >>> 0;
            h6 = (h6 + g) >>> 0;
            h7 = (h7 + h) >>> 0;
        }
        const hashBytes = [];
        const hashWords = [h0, h1, h2, h3, h4, h5, h6, h7];
        for (const word of hashWords) {
            hashBytes.push((word >>> 24) & 0xff, (word >>> 16) & 0xff, (word >>> 8) & 0xff, word & 0xff);
        }
        return _bytesToHex(hashBytes);
    }
    async function fingerprint() {
        const fp = [
            navigator.userAgent.replace(/\/[\d.]+/ig, "").replace(/\s+/g, ""),
            `${window.screen.width}x${window.screen.height}`,
            window.screen.colorDepth,
            window.devicePixelRatio,
            Intl.DateTimeFormat().resolvedOptions().timeZone,
            navigator.language,
            navigator.hardwareConcurrency,
            navigator.deviceMemory || 0,
            navigator.maxTouchPoints || 0,
            await generateAudioRenderingHex(),
            getWgslLanguageFeaturesInfo(),
            getWebGLInfo(),
            getAudioInfo(),
            getFontsInfo(),
            generateCanvasDataHex(),
            compatibleInfo()
        ];
        return getHash(fp);
    }
    return fingerprint;
})(globalThis);
